﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QuanLiSinhVien_v01.DTO;

namespace QuanLiSinhVien_v01.DAO
{
    class LopHocDao: DataProvider
    {
        public LopHocDto LopHoc { get; set; }

        protected override Object GetDataFromDataRow(DataTable dt, int rowIndex)
        {
            LopHocDto lopHocDto = new LopHocDto();
            lopHocDto.MaLopHoc = dt.Rows[rowIndex]["MaLopHoc"].ToString();
            lopHocDto.TenLopHoc = dt.Rows[rowIndex]["TenLopHoc"].ToString();
            lopHocDto.MaNganhHoc = dt.Rows[rowIndex]["MaNganhHoc"].ToString();
            lopHocDto.MaKhoaHoc = dt.Rows[rowIndex]["MaKhoaHoc"].ToString();
            lopHocDto.GhiChu = dt.Rows[rowIndex]["GhiChu"].ToString();
            
            return lopHocDto;
        }

        public LopHocDto[] GetList()
        {
            string strCommand = " Select * from LopHoc ";
            DataTable dt = ExcuteQuery(strCommand);
            int n = dt.Rows.Count;
            LopHocDto[] kq = new LopHocDto[n];
            for (int i = 0; i < n; i++)
            {
                kq[i] = (LopHocDto)GetDataFromDataRow(dt, i);
            }
            return kq;
        }
        
        public void Insert()
        {
            string strCommand = 
                "insert into LopHoc (MaLopHoc, TenLopHoc, MaKhoaHoc, MaNganhHoc, GhiChu) " +
                "values (N'" + LopHoc.MaLopHoc + "', N'" +
                              LopHoc.TenLopHoc + "', N'" +
                              LopHoc.MaKhoaHoc + "', N'" +
                              LopHoc.MaNganhHoc + "', N'" +
                              LopHoc.GhiChu + "'" +
                        ")";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool CheckExist(string MaLopHoc)
        {
            string strCommand = " Select * from LopHoc " +
                                "where MaLopHoc = N'" + MaLopHoc + "'";
            DataTable dt = ExcuteQuery(strCommand);
            if (dt.Rows.Count != 0)
                return true;
            return false;
        }
        public void Update()
        {
            string strCommand =
                "update LopHoc set " +
                    "TenLopHoc =N'" + LopHoc.TenLopHoc + "', " +
                    "MaKhoaHoc =N'" + LopHoc.MaKhoaHoc + "', " +
                    "MaNganhHoc =N'"+ LopHoc.MaNganhHoc+ "', " +
                    "GhiChu =N'"   + LopHoc.GhiChu    + "' " +
                "where MaLopHoc = '" + LopHoc.MaLopHoc + "' ";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete()
        {
            string strCommand =
                "DELETE FROM LopHoc " +
                "WHERE  MaLopHoc = '" + LopHoc.MaLopHoc + "'";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public LopHocDto[] Search(string keywork)
        {
            string strCommand = " Select * from LopHoc " +
                                "where MaLopHoc like '%" + keywork + "%' " +
                                " or TenLopHoc like '%" + keywork + "%' " +
                                " or MaKhoaHoc like '%" + keywork + "%' " +
                                " or GhiChu like '%" + keywork + "%' " +
                                " or MaNganhHoc like '%" + keywork + "%' " ;
            DataTable dt = ExcuteQuery(strCommand);
            int n = dt.Rows.Count;
            LopHocDto[] kq = new LopHocDto[n];
            for (int i = 0; i < n; i++)
            {
                kq[i] = (LopHocDto)GetDataFromDataRow(dt, i);
            }
            return kq;
        }
    }
}
